package at.hockey.ticker;

import at.hockey.ticker.R;

import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class OnFlingGestureListener extends SimpleOnGestureListener {

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	
	private ViewFlipper mVF;
	private HockeyTickerUI mHTUI;
	
	public OnFlingGestureListener(HockeyTickerUI root) {
		super();
		this.mHTUI = root;
		this.mVF = (ViewFlipper) this.mHTUI.findViewById(R.id.main_vf);
	}
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

		if(Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
			return false;
		else{
			//right to left swipe
			if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){

				Animation inFromRight = AnimationUtils.loadAnimation(mHTUI, R.anim.anim_in_from_right);
				this.mVF.setInAnimation(inFromRight);

				Animation outToLeft = AnimationUtils.loadAnimation(mHTUI, R.anim.anim_out_to_left);
				this.mVF.setOutAnimation(outToLeft);
				
				this.mVF.showNext();
				this.mHTUI.setAppTitle();
				
				return true;
			}
			//left to right swipe
			else if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){

				Animation inFromLeft = AnimationUtils.loadAnimation(mHTUI, R.anim.anim_in_from_left);
				this.mVF.setInAnimation(inFromLeft);
			 	
				Animation outToRight = AnimationUtils.loadAnimation(mHTUI, R.anim.anim_out_to_right);
				this.mVF.setOutAnimation(outToRight);
				
				this.mVF.showPrevious();
				this.mHTUI.setAppTitle();
				
				return true;
			}
			else{
				return false;
			}
		}
	}
}
